package com.gome.ocean.service.gddl.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.gome.ocean.common.exception.ClusterServiceException;
import com.gome.ocean.dao.gddl.ClusterDAO;
import com.gome.ocean.dao.gddl.ProxyDAO;
import com.gome.ocean.dao.model.gddl.ClusterBO;
import com.gome.ocean.dao.model.gddl.ProxyBO;
import com.gome.ocean.service.gddl.ClusterService;

@Service
@Transactional("transactionManager")
public class ClusterServiceImpl implements ClusterService {

    @Autowired
    private ClusterDAO clusterDAO;
    @Autowired
    private ProxyDAO   proxyDAO;

    @Override
    public boolean insertClusterBO(ClusterBO clusterBO) throws ClusterServiceException {
        List<ClusterBO> clusterBOs;
        clusterBOs = clusterDAO.findClusterBOByName(clusterBO.getName(), new RowBounds(1, 10));
        if (null != clusterBOs && !clusterBOs.isEmpty()) {
            throw new ClusterServiceException("数据库中已经存在此名称的集群");
        }
        return clusterDAO.insertClusterBO(clusterBO) == 1;
    }

    @Override
    public boolean removeClusterBOById(Long id) {
        List<ProxyBO> proxyBOs = proxyDAO.getProxyListByClusterId(id);
        if (null == proxyBOs || proxyBOs.isEmpty()) {
            return clusterDAO.removeClusterBOById(id) == 1;
        }
        return false;
    }

    @Override
    public boolean updateCluster(ClusterBO clusterBO) throws ClusterServiceException {
        List<ClusterBO> clusterBOs;
        clusterBOs = clusterDAO.findClusterBOByName(clusterBO.getName(), new RowBounds(1, 10));
        if (null == clusterBOs || clusterBOs.isEmpty()) {
            throw new ClusterServiceException("数据库中不存在此名称的集群");
        }
        return clusterDAO.updateCluster(clusterBO) == 1;
    }

    @Override
    public ClusterBO findClusterBOById(Long id) {
        return clusterDAO.findClusterBOById(id);
    }

    @Override
    public List<Long> findAllClusterIds() {
        return clusterDAO.findAllClusterIds();
    }

    @Override
    public List<ClusterBO> findAllClusterBOs() {
        return clusterDAO.findAllClusterBOs();
    }

    @Override
    public List<Long> findAllClusterIdsByProjects(List<String> projectNames) {
        return clusterDAO.findAllCluserIdByProjectNames(projectNames);
    }

    @Override
    public Page<ClusterBO> findClusterBOs(int pageNum, int pageSize) {
        return (Page<ClusterBO>) clusterDAO.findClusterBOs(new RowBounds(pageNum, pageSize));
    }

    @Override
    public List<ClusterBO> findAllClusterBOsByProjects(List<String> projectNames) {
        return clusterDAO.findAllClusterBOsByProjects(projectNames);
    }

    @Override
    public Page<ClusterBO> findClusterBOByName(String name, int pageNum, int pageSize) {
        return (Page<ClusterBO>) clusterDAO.findClusterBOByName(name, new RowBounds(pageNum, pageSize));
    }

}
